﻿define(function (require, exports, module) {


    var toString = Object.prototype.toString;

    function _isString (obj) {
        return toString.call(obj) == '[object String]';
    }

    String.Format = function () {
        for (var e = arguments[0], d = 0; d < arguments.length - 1; d++)
        {
        	e = e.replace(RegExp("\\{" + d + "\\}", "gm"),arguments[d + 1]);
        }
        return e;
    };

    function trim(str) {
        //删除左右两端的空格
        return str.replace(/(^\s*)|(\s*$)/g, '');
    }

    function xmlStrToString(xmlStr) {
        try{
            var ret = xmlStr;
            ret = ret.replace(/&amp;/g, '&');
            ret = ret.replace(/&lt;/g, '<');
            ret = ret.replace(/&gt;/g, '>');
            ret = ret.replace(/&apos;/g, '\'');
            ret = ret.replace(/&quot;/g, '\"');
            ret = ret.replace(/\r/g, '');
            ret = ret.replace(/\n/g, ' ');
            return ret;
        }catch(ex){
        }
    }

    function stringToXMLStr(Str) {
        var ret = Str;
        ret = ret.replace(/&/g, '&amp;');
        ret = ret.replace(/</g, '&lt;');
        ret = ret.replace(/>/g, '&gt;');
        ret = ret.replace(/\'/g, '&apos;');
        ret = ret.replace(/\"/g, '&quot;');
        return ret;
    }
    
    function stringToHtmlStr(Str) {
        var ret = Str;
        ret = ret.replace(/\'/g, '\\\'');
        ret = ret.replace(/&/g, '&amp;');
        ret = ret.replace(/\"/g, '&quot;');
        ret = ret.replace(/</g, '&lt;');
        ret = ret.replace(/>/g, '&gt;');
        return ret;
    }

    function getNodeFloat($obj, item_name) {
        var ret = 0;
        if ($obj && (typeof ($obj.attr(item_name)) != 'undefined')) {
            ret = parseFloat($obj.attr(item_name));
        }
        return ret;
    }

    function getNodeString($obj, item_name) {
        var ret = '';
        if ($obj && (typeof ($obj.attr(item_name)) != 'undefined')) {
            ret = xmlStrToString($obj.attr(item_name));
        }
        return ret;
    }

    WebPDF.getNodeString = getNodeString;
    WebPDF.getNodeFloat = getNodeFloat;

    function setStyleLTWH($obj, l, t, w, h) {
        if ($obj) {
            $obj.css({
                'left': l + 'px',
                'top': t + 'px',
                'width': w + 'px',
                'height': h + 'px'
            });
        }
    }

    ////////////////////////////////////////////解析及显示 Start

    var formFieldHighlightClass = 'fwr-form-field-highlight';
    var requiredFieldClass = 'fwr-form-require-field-highlight';
    var formListItemSelectedClass = 'fwr-form-listitem-selected';
    var formWidgetClass = 'fwr-form-widget';
    var formWidgetComboListClass = 'fwr-form-widget-combo-list';
    var formTextAreaNoneBorderClass = 'fwr-form-textarea-none-border';
    var formFieldsContainerClass = 'fwr-form-fields-container';

    var ConfigInfo = {
        PointToPx: (96.0 / 72.0)
    };

    var strFormatArr = {
        styleLTWH: 'left:{0}px;top:{1}px;width:{2}px;height:{3}px;'
    };

    var Type =
    {
        Unknown: 0, 		// unsupported or invalid field.
        PushButton: 1, 		// push button without any value.
        RadioButton: 2, 	// a group of radio buttons, at most one item can be selected.
        CheckBox: 3, 		// check box with on/off states.
        Text: 4, 			// single line or multi-line texts.
        RichText: 5, 		// rich text (using XML to store rich text).
        File: 6, 			// file content.
        ListBox: 7, 		// list box, single or multiple selections.
        ComboBox: 8, 		// combo box with or without the edit box.
        Sign: 9				// digital signature.
    };
    

    function FloatRect() {
        this.top = 0;
        this.left = 0;
        this.width = 0;
        this.height = 0;

        this.setValue = function (l, t, w, h) {
            this.left = l;
            this.top = t;
            this.width = w;
            this.height = h;
        };
    }

    function FormWidget() {
        this.strID = '';
        this.strBgID = '';
        this.strFgID = '';
        this.name = '';
        this.type = Type.Unknown;
        this.fieldAction = [];
        this.rect = new FloatRect();
        this.borderWidth = 0;
        this.fontSize = 12;
        this.bReadOnly = false;
        this.bRequired = false;
        this.curPageObj = null;
        this.tabNum = null;
        this.onfocus = false;
        this.parse = function (id, node, curPageObj) {
            this.strID = id;
            this.strBgID = id + '_bg';
            this.strFgID = id + '_fg';
            this.curPageObj = curPageObj;
                        
            if (node) {
                var self = this;
                if (this.curPageObj) {
                    node.children('Pos').each(function () {
                        self.rect = self.curPageObj.Transform(getNodeFloat($(this), 'l'), getNodeFloat($(this), 'b'),
                            getNodeFloat($(this), 'w'), getNodeFloat($(this), 'h'));
                    });
                }

                node.children('General').each(function () {
                    $(this).children('ReadOnly').each(function () {
                        self.bReadOnly = trim($(this).text()) == '1' ? true : false;
                    });
                    $(this).children('Required').each(function () {
                        self.bRequired = trim($(this).text()) == '1' ? true : false;
                    });
                });

                node.children('Ap').each(function () {
                    $(this).children('Font').each(function () {
                        self.fontSize = getNodeFloat($(this), 'FontSize');
                    });
                    $(this).children('BS').each(function () {
                        $(this).children('W').each(function () {
                            self.borderWidth = parseFloat($(this).text());
                        });
                    });
                });

                this.name = getNodeString(node, 'Name');
                this.type = parseInt(getNodeFloat(node, 'Type'));

                node.children('AA').each(function () {
                    $(this).children().each(function () {
                        var ht_fildAAs = [];
                        var count = 0;
                        $(this).children().each(function () {
                            var value = xmlStrToString($(this).text());
                            if (value) {
                                ht_fildAAs[count++] = value;
                            }
                        });
                        self.fieldAction[$(this)[0].tagName] = ht_fildAAs;
                    });
                });
                
                node.children('TabOrder').each(function(){
                	self.tabNum = parseInt($(this).text());
                })
            }
        };

        this.getControlID = function () {
            return this.strID;
        };

        this.getRegisterActionCode = function () {
            if (!this.bReadOnly || this.type == Type.Text || this.type == Type.RichText) {
                for (var key in this.fieldAction) {
                    var ht_aas = this.fieldAction[key];
                    for (var j = 0; j < ht_aas.length; j++) {
                        if (ht_aas[j]) {        
                            $('#' + this.strID).AddActionJS(key, ht_aas[j]);
                        }
                    }
                }
            }
        };
    }

    //PushButton
    function FormWidget_PushBtn() {
        this.formWidget = new FormWidget();
        this.value = '';
        this.NImgPos = 0;
        this.RImgPos = 0;
        this.DImgPos = 0;

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            if (node) {
                var self = this;
                node.children('Value').each(function () {
                    self.value = xmlStrToString($(this).text());
                });

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        self.NImgPos = getNodeFloat($(this), 'Path');
                    });
                    $(this).children('R').each(function () {
                        self.RImgPos = getNodeFloat($(this), 'Path');
                    });
                    $(this).children('D').each(function () {
                        self.DImgPos = getNodeFloat($(this), 'Path');
                    });
                });
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };

        this.getHtmlCode = function (bHighlight) {
            var hideStyle = 'display:none;';
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var style_fg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;',
                divLeft, divTop, divWidth, divHeight);
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;',
                    0 - this.NImgPos, 0, this.formWidget.curPageObj.allImgWidth, this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;',
                divLeft, divTop, divWidth, divHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">', formWidgetName,
                this.formWidget.strBgID, style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" tabindex=1 src="{0}" id="{1}" name="{2}" value="{3}" style="{4}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.getControlID(), formWidgetName, this.value, style_bg);
            strCode += '</div>';
            if (!bHighlight || this.formWidget.bReadOnly) {
                style_fg += hideStyle;
            }
            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>', this.formWidget.strFgID,
                this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style_fg);
            return strCode;
        };

        this.getReadyBindCode = function () {
            if (!this.formWidget.bReadOnly) {
                var controlid = this.getControlID();
                var obj = $('#' + controlid);
                obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, formWidget:this.formWidget }, PushButFocus);
                obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID,formWidget:this.formWidget }, PushButBlur);
                obj.on('mouseout', { id: controlid, curPageNum: this.formWidget.curPageObj.m_iPageNum,
                    nImgPos: this.NImgPos, rImgPos: this.RImgPos, dImgPos: this.DImgPos }, PushButMouseOut);
                obj.on('mouseover', { id: controlid, curPageNum: this.formWidget.curPageObj.m_iPageNum,
                    nImgPos: this.NImgPos, rImgPos: this.RImgPos, dImgPos: this.DImgPos }, PushButMouseOver);
                obj.on('mousemove', { id: controlid, curPageNum: this.formWidget.curPageObj.m_iPageNum,
                    nImgPos: this.NImgPos, rImgPos: this.RImgPos, dImgPos: this.DImgPos }, PushButMouseMove);
                obj.on('mousedown', { id: controlid, curPageNum: this.formWidget.curPageObj.m_iPageNum,
                    nImgPos: this.NImgPos, rImgPos: this.RImgPos, dImgPos: this.DImgPos }, PushButMouseDown);
                $('#' + this.formWidget.strFgID).on('click', { id: controlid,}, PushButFgClick);
            }
        };

        this.changeSize = function (scaleValue) {
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), divLeft, divTop, divWidth, divHeight);
            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var nImgLeft = 0 - parseInt(this.NImgPos * scaleValue + 0.5);
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            setStyleLTWH($('#' + this.getControlID()), nImgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }
        };

        this.resetData = function () {
        };

        this.update = function () {
        };
    }

    //Radio
    function FormWidget_Radio() {
        this.formWidget = new FormWidget();
        this.value = '';
        this.bCheck = false;
        this.bRadiosInUnison = false;
        this.bNoToggleToOff = true; //不可以取消选中项
        this.offImgPos = 0;
        this.onImgPos = 0;

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            if (node) {
                var self = this;
                node.children('Value').each(function () {
                    self.value = xmlStrToString($(this).text());
                });

                node.children('State').each(function () {
                    $(this).children('Checked').each(function () {
                        self.bCheck = parseInt(getNodeFloat($(this), 'Checked')) > 0 ? true : false;
                    });
                    $(this).children('Unison').each(function () {
                        self.bRadiosInUnison = parseInt(getNodeFloat($(this), 'Unison')) > 0 ? true : false;
                    });
                });

                //获取bNoToggleToOff标志**待完善

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        $(this).children('Off').each(function () {
                            self.offImgPos = getNodeFloat($(this), 'Path');
                        });

                        $(this).children('On').each(function () {
                            self.onImgPos = getNodeFloat($(this), 'Path');
                        });
                    });
                });

                if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.bCheck ? this.value : 'Off');
                } else if (this.bCheck && WebPDF.g_formValue.GetItemValue(this.formWidget.name) == 'Off') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
                }
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };

        this.getHtmlCode = function (bHighlight) {
            var hideStyle = 'display:none;';
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var style_fg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;',
                divLeft, divTop, divWidth, divHeight);
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;',
                this.bCheck ? 0 - this.onImgPos : 0 - this.offImgPos, 0, this.formWidget.curPageObj.allImgWidth,
                this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;',
                divLeft, divTop, divWidth, divHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">', formWidgetName, this.formWidget.strBgID,
                style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" tabindex=1 src="{0}" id="{1}" name="{2}" checked="{3}" value="{4}" style="{5}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.getControlID(), formWidgetName, this.bCheck ? '1' : '0', this.value, style_bg);
            strCode += '</div>';
            if (!bHighlight || this.formWidget.bReadOnly) {
                style_fg += hideStyle;
            }
            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>',
                this.formWidget.strFgID, this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style_fg);
            return strCode;
        };

        this.getReadyBindCode = function () {
            if (!this.formWidget.bReadOnly) {
                var controlid = this.getControlID();
                var obj = $('#' + controlid);
                obj.on('click', { id: controlid, bNoToggleToOff: this.bNoToggleToOff }, RaidoButClick);
                obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID,formWidget:this.formWidget }, CheckRaidoButFocus);
                obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID,formWidget:this.formWidget }, CheckRaidoButBlur);
                $('#' + this.formWidget.strFgID).on('click', { id: controlid }, CheckRaidoButFgClick);
            }
        };

        this.changeSize = function (scaleValue) {
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), divLeft, divTop, divWidth, divHeight);
            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var imgLeft = this.offImgPos;
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            if ($('#' + this.getControlID()).attr('isChecked') == 1) {
                imgLeft = this.onImgPos;
            }
            imgLeft = 0 - parseInt(imgLeft * scaleValue + 0.5);
            setStyleLTWH($('#' + this.getControlID()), imgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }
        };

        this.resetData = function () {
            if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.bCheck ? this.value : 'Off');
            }
            else if (this.bCheck && WebPDF.g_formValue.GetItemValue(this.formWidget.name) == 'Off') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
            }
            CheckedObj(this, this.bCheck);
        };

        this.update = function () {
            var selectedItemID = arguments[0] ? arguments[0] : '';
            var value = WebPDF.g_formValue.GetItemValue(this.formWidget.name);
            var bCheck = false;
            if (typeof (value) != 'undefined') {
                if (!selectedItemID || selectedItemID == '') {
                    selectedItemID = this.formWidget.curPageObj.GetRadioGroupSelectedItemID(this.formWidget.name);
                }

                bCheck = this.value == value;
                if (selectedItemID && selectedItemID != '' && (!this.bRadiosInUnison)) {
                    bCheck = bCheck && (selectedItemID == this.getControlID());
                }
            }
            CheckedObj(this, bCheck);
        };
    }

    //Check
    function FormWidget_Check() {
        this.formWidget = new FormWidget();
        this.value = '';
        this.bCheck = false;
        this.offImgPos = 0;
        this.onImgPos = 0;

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            if (node) {
                var self = this;
                node.children('Value').each(function () {
                    self.value = xmlStrToString($(this).text());
                });

                node.children('State').each(function () {
                    $(this).children('Checked').each(function () {
                        self.bCheck = parseInt(getNodeFloat($(this), 'Checked')) > 0 ? true : false;
                    });
                });

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        $(this).children('Off').each(function () {
                            self.offImgPos = getNodeFloat($(this), 'Path');
                        });

                        $(this).children('On').each(function () {
                            self.onImgPos = getNodeFloat($(this), 'Path');
                        });
                    });
                });

                if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.bCheck ? this.value : 'Off');
                } else if (this.bCheck && WebPDF.g_formValue.GetItemValue(this.formWidget.name) == 'Off') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
                }
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };
        this.getHtmlCode = function (bHighlight) {
            var hideStyle = 'display:none;';
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var style_fg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;', divLeft,
                divTop, divWidth, divHeight);
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;cursor:pointer;',
                this.bCheck ? 0 - this.onImgPos : 0 - this.offImgPos, 0, this.formWidget.curPageObj.allImgWidth, this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;',
                divLeft, divTop, divWidth, divHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">', formWidgetName, this.formWidget.strBgID,
                style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" tabindex=1 src="{0}" id="{1}" name="{2}" checked="{3}" value="{4}" style="{5}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.getControlID(), formWidgetName, this.bCheck ? '1' : '0', this.value, style_bg);
            strCode += '</div>';
            if (!bHighlight || this.formWidget.bReadOnly) {
                style_fg += hideStyle;
            }
            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>', this.formWidget.strFgID,
                this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style_fg);
            return strCode;
        };

        this.getReadyBindCode = function () {
            if (!this.formWidget.bReadOnly) {
                var contolid = this.getControlID();
                var obj = $('#' + contolid);
                obj.on('click', { id: contolid }, CheckButClick);
                obj.on('focus', { id: contolid, fgid: this.formWidget.strFgID, formWidget:this.formWidget }, CheckRaidoButFocus);
                obj.on('blur', { id: contolid, fgid: this.formWidget.strFgID, formWidget:this.formWidget }, CheckRaidoButBlur);
                $('#' + this.formWidget.strFgID).on('click', { id: contolid }, CheckRaidoButFgClick);
            }
        };

        this.changeSize = function (scaleValue) {
            var divLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), divLeft, divTop, divWidth, divHeight);
            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var imgLeft = this.offImgPos;
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            if ($('#' + this.getControlID()).attr('isChecked') == 1) {
                imgLeft = this.onImgPos;
            }
            imgLeft = 0 - parseInt(imgLeft * scaleValue + 0.5);
            setStyleLTWH($('#' + this.getControlID()), imgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }

        };

        this.resetData = function () {
            if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.bCheck ? this.value : 'Off');
            } else if (this.bCheck && WebPDF.g_formValue.GetItemValue(this.formWidget.name) == 'Off') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
            }
            CheckedObj(this, this.bCheck);
        };

        this.update = function () {
            var value = WebPDF.g_formValue.GetItemValue(this.formWidget.name);

            if (typeof (value) != 'undefined') {
                var bCheck = this.value == value;
                CheckedObj(this, bCheck);
            }
        };
    }

    //Text
    function FormWidget_Text() {
        this.formWidget = new FormWidget();
        this.bMultiLine = false;
        this.bSpellCheck = false;
        this.imgPos = 0;
        this.value = '';

        this.bUnbind = true;

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            if (this.formWidget.borderWidth == 0) {
                this.formWidget.borderWidth = 1;
            }
            if (node) {
                var self = this;
                node.children('Value').each(function () {
                    self.value = xmlStrToString($(this).text());
                });

                node.children('Flags').each(function () {
                    $(this).children('MultiLine').each(function () {
                        self.bMultiLine = trim($(this).text()) == '1' ? true : false;
                    });
                    $(this).children('SpellCheck').each(function () {
                        self.bSpellCheck = trim($(this).text()) == '1' ? true : false;
                    });
                });

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        self.imgPos = getNodeFloat($(this), 'Path');
                    });
                });

                if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
                }
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };
        this.getHtmlCode = function (bHighlight) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx + 0.5);
            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var divFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx + 0.5);
            } else {
                if (this.bMultiLine) {
                    divFontSize = parseInt(12 * ConfigInfo.PointToPx + 0.5);
                } else {
                    if (this.formWidget.curPageObj.rotate == 1 || this.formWidget.curPageObj.rotate == 3) {
                        divFontSize = parseInt(this.formWidget.rect.width * 0.7 * ConfigInfo.PointToPx + 0.5);
                    } else {
                        divFontSize = parseInt(this.formWidget.rect.height * 0.7 * ConfigInfo.PointToPx + 0.5);
                    }
                }
            }
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;', 0 - this.imgPos, 0,
                this.formWidget.curPageObj.allImgWidth, this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;',
                divBgLeft, divBgTop, divBgWidth, divBgHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">',
            		formWidgetName, this.formWidget.strBgID + '_div', style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" src="{0}" id="{1}" style="{2}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.formWidget.strBgID, style_bg);
            strCode += '</div>';

            if (this.bMultiLine) {
                var style = String.Format(strFormatArr.styleLTWH + 'line-height:100%;max-width:{2}px;max-height:{3}px;position:absolute;background-image:url(about:blank);background-color:transparent;font-size:{4}px;resize:none;border:0;padding:0;',
                    divLeft, divTop, divWidth, divHeight, divFontSize);
                strCode += String.Format('<textarea id="{0}" name="{1}" style="{2}" class="fwr_form_textarea_none_border fwr-form-widget" multiline="1" {3}>{4}</textarea>',
                    this.getControlID(), formWidgetName, style, this.formWidget.bReadOnly ? 'readonly="readonly"' : '', this.value);
            } else {
                var style = String.Format(strFormatArr.styleLTWH + 'line-height:{3}px;position:absolute;font-size:{4}px;background-image:url(about:blank);background-color:transparent;border:0;padding:0;',
                    divLeft, divTop, divWidth, divHeight, divFontSize);
                strCode += String.Format('<input type="text" class="fwr-form-widget" id="{0}" name="{1}" style="{2}" multiline="0" value="{3}" {4}/>',
                    this.getControlID(), formWidgetName, style, this.value, this.formWidget.bReadOnly ? 'readonly="readonly"' : '');
            }

            var style_fg = String.Format(strFormatArr.styleLTWH + 'cursor:text;', divLeft, divTop, divWidth, divHeight);
            if (!bHighlight || this.formWidget.bReadOnly) {
                style_fg += 'display:none';
            }
            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>', this.formWidget.strFgID,
                this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style_fg);
            return strCode;
        };

        this.getReadyBindCode = function () {
            var controlid = this.getControlID();
            var obj = $('#' + controlid);
            if (this.formWidget.bReadOnly) {
                if (this.bMultiLine) {
                    obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, bReadOnly: this.formWidget.bReadOnly,
                        bSpellCheck: this.bSpellCheck, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextareaFocus);
                    obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID, bReadOnly: this.formWidget.bReadOnly,
                        bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextareaBlur);
                }
            } else {
                if (this.bMultiLine) {
                    obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, bReadOnly: this.formWidget.bReadOnly,
                        bSpellCheck: this.bSpellCheck, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextareaFocus);
                    obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID, bReadOnly: this.formWidget.bReadOnly,
                        bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextareaBlur);
                } else {
                    obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextFocus);
                    obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, TextBlur);
                }

                if (WebPDF.Environment.mobile || WebPDF.Environment.iOS) {
                    $('#' + this.formWidget.strFgID).on('touchstart.jsp', { id: controlid }, FgClick);
                } else {
                    $('#' + this.formWidget.strFgID).on('click', { id: controlid }, FgClick);
                }

                if (this.bSpellCheck) {
                    obj.on('keypress', { id: controlid, bTextarea: this.bMultiLine }, TextSpellCeck);
                    obj.on('paste', { id: controlid, bTextarea: this.bMultiLine }, TextPaste);
                }
            }
        };

        this.changeSize = function (scaleValue) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var divFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx * scaleValue + 0.5);
            } else {
                if (this.bMultiLine) {
                    divFontSize = parseInt(12 * ConfigInfo.PointToPx * scaleValue + 0.5);
                } else {
                    if (this.formWidget.curPageObj.rotate == 1 || this.formWidget.curPageObj.rotate == 3) {
                        divFontSize = parseInt(this.formWidget.rect.width * 0.7 * ConfigInfo.PointToPx * scaleValue + 0.5);
                    } else {
                        divFontSize = parseInt(this.formWidget.rect.height * 0.7 * ConfigInfo.PointToPx * scaleValue + 0.5);
                    }
                }
            }

            setStyleLTWH($('#' + this.formWidget.strBgID + '_div'), divBgLeft, divBgTop, divBgWidth, divBgHeight);
            if (this.bMultiLine) {
                var obj = $('#' + this.getControlID());
                obj.css('max-width', divWidth + 'px').css('max-height', divHeight + 'px').css('font-size', divFontSize + 'px');
                setStyleLTWH(obj, divLeft, divTop, divWidth, divHeight);
            } else {
                var obj = $('#' + this.getControlID());
                obj.css('line-height', divHeight + 'px').css('font-size', divFontSize + 'px');
                setStyleLTWH(obj, divLeft, divTop, divWidth, divHeight);
            }
            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var imgLeft = 0 - parseInt(this.imgPos * scaleValue + 0.5);
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), imgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }
        };

        this.resetData = function () {
            if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
            }
            this.bUnbind = true;
            var obj = $('#' + this.getControlID());
            if (this.bMultiLine) {
                obj.val(this.value);
            } else {
                obj.attr('value', this.value);
            }
        };

        this.update = function () {
            var curItemID = arguments[0] ? arguments[0] : '';
            if (curItemID == this.getControlID()) {
                return;
            }

            var obj = $('#' + this.getControlID());
            if(obj.is(":focus")){
                return;
            }
            if (!this.formWidget.bReadOnly || this.bUnbind) {
                var value = WebPDF.g_formValue.GetItemValue(this.formWidget.name);
                if (typeof (value) == 'undefined') {
                    value = '';
                }
                obj.val(value);
            }

            obj.attr('noupdate', 'true').trigger('blur').trigger('C');
            if (this.formWidget.bReadOnly && this.bUnbind) {
                this.bUnbind = false;
                setTimeout(function () {
                    obj.off();
                    if (this.bMultiLine) {
                        $('#' + this.getControlID()).on('focus', { id: this.getControlID(), fgid: this.formWidget.strFgID,
                            bReadOnly: this.formWidget.bReadOnly, bSpellCheck: this.bSpellCheck }, TextareaFocus);
                        $('#' + this.getControlID()).on('blur', { id: this.getControlID(), fgid: this.formWidget.strFgID,
                            bReadOnly: this.formWidget.bReadOnly }, TextareaBlur);
                    }
                }, 1000);
            }
        };
    }

    //List
    function Option() {
        this.strID = '';
        this.bSelected = false;
        this.realValue = '';
        this.text = '';
    }

    function FormWidget_List() {
        this.formWidget = new FormWidget();
        this.value = '';
        this.bMultiple = false;
        this.imgPos = 0;
        this.optios = [];

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            if (node) {
                var self = this;
                node.children('Options').each(function () {
                    var index = 0;
                    $(this).children('Option').each(function () {
                        var option = new Option();
                        option.strID = id + '_' + index.toString();
                        option.text = xmlStrToString($(this).text());
                        option.realValue = getNodeString($(this), 'realValue');
                        if (option.realValue == '') {
                            option.realValue = option.text;
                        }
                        option.bSelected = parseInt(getNodeFloat($(this), 'Selected')) > 0 ? true : false;
                        self.optios[index] = option;
                        index++;

                        if (option.bSelected) {
                            if (self.value != '') {
                                self.value += ';';
                            }
                            self.value += option.realValue;
                        }
                    });
                });

                node.children('Flags').each(function () {
                    $(this).children('MultiSelect').each(function () {
                        self.bMultiple = trim($(this).text()) == '1' ? true : false;
                    });
                });

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        self.imgPos = getNodeFloat($(this), 'Path');
                    });
                });

                if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
                }
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };
        this.getHtmlCode = function (bHighlight) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx + 0.5);
            var divFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx + 0.5);
            } else {
                divFontSize = parseInt(12 * ConfigInfo.PointToPx + 0.5);
            }
            var strMultiple = this.bMultiple ? 'true' : 'false';
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;', 0 - this.imgPos, 0,
                this.formWidget.curPageObj.allImgWidth, this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;',
                divBgLeft, divBgTop, divBgWidth, divBgHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">',
            		formWidgetName, this.formWidget.strBgID + '_div', style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" src="{0}" id="{1}" style="{2}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.formWidget.strBgID, style_bg);
            strCode += '</div>';

            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;

            var style = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:url(about:blank);background-color:transparent;overflow:auto;-moz-user-select:none;',
                divLeft, divTop, divWidth, divHeight);
            strCode += String.Format('<div class="fwr-form-widget" id="{0}" name="{1}" style="{2}" fxmultiple="{3}" tabindex=1>',
                this.getControlID(), formWidgetName, style, strMultiple);

            for (var index = 0; index < this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    strCode += String.Format('<div id="{0}" tabindex=2 style="overflow: hidden; display: block;width:100%;font-size:{1}px;cursor:default;" class="{2} fwr-form-widget" selected="{3}" realValue="{4}">{5}</div>',
                        tmp.strID, divFontSize, tmp.bSelected ? formListItemSelectedClass : '', tmp.bSelected ? '1' : '0', tmp.realValue, tmp.text);
                }
            }

            strCode += '</div>';
            style = String.Format(strFormatArr.styleLTWH, divLeft, divTop, divWidth, divHeight);
            style += 'cursor:default;';
            if (!bHighlight || this.formWidget.bReadOnly) {
                style += 'display:none';
            }
            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>', this.formWidget.strFgID, this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style);
            return strCode;
        };

        this.getReadyBindCode = function () {
            if (!this.formWidget.bReadOnly) {
                var controlid = this.getControlID();
                var obj = $('#' + controlid);
                for (var i = 0; i < this.optios.length; i++) {
                    var tmp = this.optios[i];
                    if (tmp) {
                        var itemObj = $('#' + tmp.strID);
                        itemObj.on('click', { id: tmp.strID, listdivid: controlid }, ListItemClick);
                        itemObj.on('keydown', { id: tmp.strID, listdivid: controlid }, ListItemKeydown);
                        itemObj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID }, ListFocus);
                        itemObj.on('blur', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID }, ListBlur);
                    }
                }
                //取消文本选择
                obj.on('selectstart', function () {
                    return false;
                }).trigger('selectstart');
                obj.on('keydown', { id: controlid }, ListKeyDown);
                obj.on('focus', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, ListFocus);
                obj.on('blur', { id: controlid, fgid: this.formWidget.strFgID, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, ListBlur);
                $('#' + this.formWidget.strFgID).on('click', { id: controlid, fgid: this.formWidget.strFgID,
                    optiosCount: this.optios.length }, ListBoxFgClick);
            }
        };

        this.changeSize = function (scaleValue) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var divFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx * scaleValue + 0.5);
            } else {
                divFontSize = parseInt(12 * ConfigInfo.PointToPx * scaleValue + 0.5);
            }

            setStyleLTWH($('#' + this.formWidget.strBgID + '_div'), divBgLeft, divBgTop, divBgWidth, divBgHeight);

            var obj = $('#' + this.getControlID());
            setStyleLTWH(obj, divLeft, divTop, divWidth, divHeight);
            for (var index = 0; index < this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    obj = $('#' + tmp.strID);
                    obj.css('font-size', divFontSize + 'px');
                }
            }

            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var imgLeft = 0 - parseInt(this.imgPos * scaleValue + 0.5);
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), imgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }
        };

        this.resetData = function () {
            if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.value);
            }

            for (var index = 0; index < this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    SelectComboItem($('#' + tmp.strID), tmp.bSelected);
                }
            }
        };

        this.update = function () {
            var valueArr = null;
            var fieldValue = WebPDF.g_formValue.GetItemValue(this.formWidget.name);
            if (fieldValue && fieldValue != '') {
                valueArr = fieldValue.split(';');
            }

            for (var index = 0; index < this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    var bSelected = false;
                    for (i in valueArr) {
                        if (trim(valueArr[i]) == tmp.realValue) {
                            bSelected = true;
                            break;
                        }
                    }
                    SelectComboItem($('#' + tmp.strID), bSelected);
                }
            }
        };
    }

    //Combo
    function FormWidget_Combo() {
        this.formWidget = new FormWidget();
        this.listDivID = '';
        this.imgPos = 0;
        this.bEdit = false;
        this.optios = [];
        this.selectValue = '';
        this.selectText = '';
        this.value = '';
        this.inputID = '';

        this.parser = function (id, node, curPageObj) {
            this.formWidget.parse(id, node, curPageObj);
            this.listDivID = id + '_list';
            this.inputID = id + '_input';
            if (node) {
                var self = this;
                node.children('Options').each(function () {
                    var index = 0;
                    $(this).children('Option').each(function () {
                        var option = new Option();
                        option.strID = self.listDivID + '_' + index.toString();
                        option.text = xmlStrToString($(this).text());
                        option.realValue = getNodeString($(this), 'realValue');
                        if (option.realValue == '') {
                            option.realValue = option.text;
                        }
                        option.bSelected = parseInt(getNodeFloat($(this), 'Selected')) > 0 ? true : false;
                        self.optios[index] = option;
                        index++;

                        if (option.bSelected) {
                            self.selectValue = option.realValue;
                            self.selectText = option.text;
                        }
                    });
                });

                node.children('Value').each(function () {
                    self.value = trim($(this).text());
                });

                node.children('General').each(function () {
                    $(this).children('Edit').each(function () {
                        self.bEdit = trim($(this).text()) == '1' ? true : false;
                    });
                });

                node.children('Ap').each(function () {
                    $(this).children('N').each(function () {
                        self.imgPos = getNodeFloat($(this), 'Path');
                    });
                });

                if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                    WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.selectValue ? this.selectValue : this.value);
                }
            }
        };

        this.getControlID = function () {
            return this.formWidget.getControlID();
        };
        this.getHtmlCode = function (bHighlight) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx + 0.5);
            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var divFontSize = 0;
            var listFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx + 0.5);
                listFontSize = divFontSize;
            } else {
                if (this.formWidget.curPageObj.rotate == 1 || this.formWidget.curPageObj.rotate == 3) {
                    divFontSize = parseInt(this.formWidget.rect.width * 0.7 * ConfigInfo.PointToPx + 0.5);
                } else {
                    divFontSize = parseInt(this.formWidget.rect.height * 0.7 * ConfigInfo.PointToPx + 0.5);
                }
                listFontSize = parseInt(12 * ConfigInfo.PointToPx + 0.5);
            }
            var listDivTop = divBgTop + divBgHeight;
            var style_bg = String.Format(strFormatArr.styleLTWH + 'position:absolute;padding:0;', 0 - this.imgPos, 0, this.formWidget.curPageObj.allImgWidth, this.formWidget.curPageObj.allImgHeight);
            var style_bgdiv = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-image:none;overflow:hidden;', divBgLeft, divBgTop, divBgWidth, divBgHeight);
            var formWidgetName = stringToHtmlStr(this.formWidget.name);
            var strCode = String.Format('<div name="{0}"><div id="{1}" style="{2}">', formWidgetName, this.formWidget.strBgID + '_div', style_bgdiv);
            strCode += String.Format('<img class="fwr-form-widget" src="{0}" id="{1}" style="{2}"/>',
                this.formWidget.curPageObj.m_imgUrl, this.formWidget.strBgID, style_bg);
            strCode += '</div>';

            var style = String.Format(strFormatArr.styleLTWH + 'line-height:{3}px;position:absolute;font-size:{4}px;text-align:center;padding:0;background-image:url(about:blank);',
                divLeft, divTop, divWidth, divHeight, divFontSize);
            strCode += String.Format('<div class="fwr-form-widget" id="{0}" name="{1}" style="{2}" tabindex=1></div>', this.getControlID(), formWidgetName, style);

            var tmpdivLeft = divLeft;
            var tmpdivTop = divTop;
            var tmpdivWidth = divWidth;
            var tmpdivHeight = divHeight;
            var rightWidth = parseInt(16 * ConfigInfo.PointToPx + 0.5);
            switch (this.formWidget.curPageObj.rotate) {
                case 1:
                    //90
                {
                    tmpdivHeight = tmpdivHeight - rightWidth;
                }
                    break;
                case 2:
                    //180
                {
                    tmpdivWidth = tmpdivWidth - rightWidth;
                    tmpdivLeft = tmpdivLeft + rightWidth;
                }
                    break;
                case 3:
                    //270
                {
                    tmpdivHeight = tmpdivHeight - rightWidth;
                    tmpdivTop = tmpdivTop + rightWidth;
                }
                    break;
                default:
                    //0
                {
                    tmpdivWidth = tmpdivWidth - rightWidth;
                }
            }

            style = String.Format(strFormatArr.styleLTWH + 'line-height:{3}px;position:absolute;font-size:{4}px;background-image:url(about:blank);background-color:transparent;padding:0;border:0;',
                tmpdivLeft, tmpdivTop, tmpdivWidth, tmpdivHeight, divFontSize);
            strCode += String.Format('<input class="fwr-form-widget" type="text" id="{0}" style="{1}" value="{2}" oldValue="{2}" realValue="{3}" {4}/>',
                this.inputID, style, this.selectValue ? this.selectText : this.value, this.selectValue ? this.selectValue : this.value, (this.formWidget.bReadOnly || !this.bEdit) ? 'readonly="readonly"' : '');

            style = String.Format('left:{0}px;top:{1}px;width:{2}px;position: absolute;background-color:#ffffff;border: 1px solid #6ea3c4;display:none;overflow:auto;',
                divBgLeft, listDivTop, divBgWidth);
            strCode += String.Format('<div id="{0}" style="{1}" class="fwr-form-widget-combo-list" tabindex=2>', this.listDivID, style);
            for (var index = 0; index <= this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    strCode += String.Format('<div id="{0}" style="overflow: hidden;display: block;width:100%;font-size:{1}px;" tabindex=3 class="fwr_form_combobox_item {2}" selected="{3}"  realValue="{4}">{5}</div>',
                        tmp.strID, listFontSize, tmp.bSelected ? formListItemSelectedClass : '', tmp.bSelected ? '1' : '0', tmp.realValue, tmp.text);
                }
            }
            strCode += '</div>';

            style = String.Format(strFormatArr.styleLTWH, divLeft, divTop, divWidth, divHeight);
            style += 'cursor:default;';
            if (!bHighlight || this.formWidget.bReadOnly) {
                style += 'display:none';
            }

            strCode += String.Format('<div id="{0}" class="{1} fwr-form-widget" style="{2}"></div></div>', this.formWidget.strFgID, this.formWidget.bRequired ? requiredFieldClass : formFieldHighlightClass, style);
            return strCode;
        };

        this.getReadyBindCode = function () {
            if (!this.formWidget.bReadOnly) {
                var controlid = this.getControlID();
                $('#' + controlid).on('focus', { inputid: this.inputID, formWidget:this.formWidget }, ComboButFocusOrFgFocus);
                $('#' + controlid).on('click', { inputid: this.inputID, formWidget:this.formWidget }, ComboButFocusOrFgClick);
                $('#' + this.inputID).on('focus', { id: controlid, listdivid: this.listDivID, fgid: this.formWidget.strFgID, inputid: this.inputID, bgid: this.formWidget.strBgID }, ComboButIputFocus);
                $('#' + this.inputID).on('click', { listdivid: this.listDivID, fgid: this.formWidget.strFgID }, ComboButIputClick);
                $('#' + this.inputID).on('blur', { id: controlid, listdivid: this.listDivID, fgid: this.formWidget.strFgID, inputid: this.inputID, bgid: this.formWidget.strBgID, formWidget:this.formWidget }, ComboButIputBlur);
                $('#' + this.listDivID).on('blur', { id: controlid, listdivid: this.listDivID, fgid: this.formWidget.strFgID, inputid: this.inputID, bgid: this.formWidget.strBgID }, ComboButIputBlur);
                for (var i = 0; i < this.optios.length; i++) {
                    var tmp = this.optios[i];
                    if (tmp) {
                        $('#' + tmp.strID).on('click', { id: tmp.strID, combodivid: controlid, listdivid: this.listDivID, fgid: this.formWidget.strFgID, inputid: this.inputID, bgid: this.formWidget.strBgID }, ComboListItemClick);
                        $('#' + tmp.strID).on('blur', { id: controlid, listdivid: this.listDivID, fgid: this.formWidget.strFgID, inputid: this.inputID, bgid: this.formWidget.strBgID }, ComboButIputBlur);
                    }
                }
                $('#' + this.formWidget.strFgID).on('click', { inputid: this.inputID, formWidget:this.formWidget }, ComboButFocusOrFgClick);
            }
        };

        this.changeSize = function (scaleValue) {
            var divBgLeft = parseInt(this.formWidget.rect.left * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgTop = parseInt(this.formWidget.rect.top * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgWidth = parseInt(this.formWidget.rect.width * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBgHeight = parseInt(this.formWidget.rect.height * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divBorderWidth = parseInt(this.formWidget.borderWidth * ConfigInfo.PointToPx * scaleValue + 0.5);
            var divLeft = divBgLeft + divBorderWidth;
            var divTop = divBgTop + divBorderWidth;
            var divWidth = divBgWidth - 2 * divBorderWidth;
            var divHeight = divBgHeight - 2 * divBorderWidth;
            var listDivTop = divBgTop + divBgHeight;
            var divFontSize = 0;
            var listFontSize = 0;
            if (this.formWidget.fontSize != -1) {
                divFontSize = parseInt(this.formWidget.fontSize * ConfigInfo.PointToPx * scaleValue + 0.5);
                listFontSize = divFontSize;
            } else {
                if (this.formWidget.curPageObj.rotate == 1 || this.formWidget.curPageObj.rotate == 3) {
                    divFontSize = parseInt(this.formWidget.rect.width * 0.7 * ConfigInfo.PointToPx * scaleValue + 0.5);
                } else {
                    divFontSize = parseInt(this.formWidget.rect.height * 0.7 * ConfigInfo.PointToPx * scaleValue + 0.5);
                }
                listFontSize = parseInt(12 * ConfigInfo.PointToPx * scaleValue + 0.5);
            }

            setStyleLTWH($('#' + this.formWidget.strBgID + '_div'), divBgLeft, divBgTop, divBgWidth, divBgHeight);

            var obj = $('#' + this.getControlID());
            obj.css('line-height', divHeight + 'px').css('font-size', divFontSize + 'px');
            setStyleLTWH(obj, divLeft, divTop, divWidth, divHeight);

            var tmpdivLeft = divLeft;
            var tmpdivTop = divTop;
            var tmpdivWidth = divWidth;
            var tmpdivHeight = divHeight;
            var rightWidth = parseInt(16 * ConfigInfo.PointToPx * scaleValue + 0.5);
            switch (this.formWidget.curPageObj.rotate) {
                case 1:
                    //90
                {
                    tmpdivHeight = tmpdivHeight - rightWidth;
                }
                    break;
                case 2:
                    //180
                {
                    tmpdivWidth = tmpdivWidth - rightWidth;
                    tmpdivLeft = tmpdivLeft + rightWidth;
                }
                    break;
                case 3:
                    //270
                {
                    tmpdivHeight = tmpdivHeight - rightWidth;
                    tmpdivTop = tmpdivTop + rightWidth;
                }
                    break;
                default:
                    //0
                {
                    tmpdivWidth = tmpdivWidth - rightWidth;
                }
            }
            obj = $('#' + this.inputID);
            obj.css('line-height', tmpdivHeight + 'px').css('font-size', divFontSize + 'px');
            setStyleLTWH(obj, tmpdivLeft, tmpdivTop, tmpdivWidth, tmpdivHeight);

            obj = $('#' + this.listDivID);
            obj.css('left', divBgLeft + 'px').css('top', listDivTop + 'px').css('width', divBgWidth + 'px');

            for (var index = 0; index <= this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    $('#' + tmp.strID).css('font-size', listFontSize + 'px');
                }
            }

            setStyleLTWH($('#' + this.formWidget.strFgID), divLeft, divTop, divWidth, divHeight);

            var imgLeft = 0 - parseInt(this.imgPos * scaleValue + 0.5);
            var imgWidth = parseInt(this.formWidget.curPageObj.allImgWidth * scaleValue + 0.5);
            var imgHeight = parseInt(this.formWidget.curPageObj.allImgHeight * scaleValue + 0.5);
            setStyleLTWH($('#' + this.formWidget.strBgID), imgLeft, 0, imgWidth, imgHeight);
        };

        this.changeHighlight = function (bHighlight) {
            if (!this.formWidget.bReadOnly) {
                $('#' + this.formWidget.strFgID).css('display', (bHighlight ? '' : 'none'));
            }
        };

        this.resetData = function () {
            if (typeof (WebPDF.g_formValue.GetItemValue(this.formWidget.name)) == 'undefined') {
                WebPDF.g_formValue.SetItemValue(this.formWidget.name, this.selectValue ? this.selectValue : this.value);
            }
            for (var index = 0; index <= this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    SelectComboItem($('#' + tmp.strID), tmp.bSelected);
                }
            }
        };

        this.update = function () {
            var fieldValue = WebPDF.g_formValue.GetItemValue(this.formWidget.name);
            if (typeof (fieldValue) == 'undefined') {
                fieldValue = '';
            }

            var inputObj = $('#' + this.inputID);
            inputObj.attr('realValue', fieldValue);
            inputObj.attr('oldValue', fieldValue);
            inputObj.val(fieldValue);

            for (var index = 0; index <= this.optios.length; index++) {
                var tmp = this.optios[index];
                if (tmp) {
                    var bSelected = tmp.realValue == fieldValue;
                    if (bSelected) {
                        inputObj.attr('oldValue', tmp.text);
                        inputObj.val(tmp.text);
                    }
                    SelectComboItem($('#' + tmp.strID), bSelected);
                }
            }
        };
    }

    function CreateWidget(id, node, curPageObj) {
        var widget = null;
        var type;

        if (node) {
            type = parseInt(getNodeFloat(node, 'Type'));

            if (type == Type.PushButton) {
                widget = new FormWidget_PushBtn();
                widget.parser(id, node, curPageObj);
            } else if (type == Type.RadioButton) {
                widget = new FormWidget_Radio();
                widget.parser(id, node, curPageObj);
            } else if (type == Type.CheckBox) {
                widget = new FormWidget_Check();
                widget.parser(id, node, curPageObj);
            } else if (type == Type.Text || type == Type.RichText) {
                widget = new FormWidget_Text();
                widget.parser(id, node, curPageObj);
            } else if (type == Type.ListBox) {
                widget = new FormWidget_List();
                widget.parser(id, node, curPageObj);
            } else if (type == Type.ComboBox) {
                widget = new FormWidget_Combo();
                widget.parser(id, node, curPageObj);
            }
        }
        return widget;
    }

    //FormPage
    function FormPage(readerApp) {
        this.scale = 1;
        this.strID = '';
        this.strBgID = '';
        this.pdfRect = new FloatRect();
        this.rotate = 0;
        this.pageWidth = 0;
        this.pageHeight = 0;
        this.imgPos = 0;
        this.allImgWidth = 0;
        this.allImgHeight = 0;
        this.m_imgUrl = '';
        this.formWidgets = [];
        this.m_iPageNum = 0;
        this.m_bLastPage = false;
        this.divWidth = 0;
        this.divHeight = 0;

        this.parserLastPage = function (id, pageNo, divWidth, divHeight) {
            this.m_bLastPage = true;
            this.strID = id;
            this.m_iPageNum = pageNo;
            this.divWidth = divWidth;
            this.divHeight = divHeight;
        };

        this.parser = function (id, pageNo, node) {
            this.strID = id;
            this.strBgID = id + '_bg';
            this.m_iPageNum = pageNo;
            this.m_imgUrl = readerApp.getOptions().url + 'form/bg/' + this.m_iPageNum + "?s3Key=" + readerApp.getFileID();;
            if (node) {
                var self = this;
                node.each(function () {
                    $(this).children('State').each(function () {
                        self.rotate = parseInt(getNodeFloat($(this), 'Rotate'));
                        self.pdfRect.setValue(getNodeFloat($(this), 'l'), getNodeFloat($(this), 'b'), getNodeFloat($(this), 'Width'), getNodeFloat($(this), 'Height'));
                        $(this).children('Ap').each(function () {
                            self.imgPos = getNodeFloat($(this), 'Path');
                        });
                    });
                    $(this).children('ImgInfo').each(function () {
                        self.allImgWidth = getNodeFloat($(this), 'Width');
                        self.allImgHeight = getNodeFloat($(this), 'Height');
                    });
                });

                switch (this.rotate) {
                    case 1:
                    case 3:
                        //90 or 270
                    {
                        this.pageHeight = this.pdfRect.width;
                        this.pageWidth = this.pdfRect.height;
                    }
                        break;
                    case 2:
                    default:
                        //0 or 180
                    {
                        this.pageHeight = this.pdfRect.height;
                        this.pageWidth = this.pdfRect.width;
                    }
                }

                var curFormPageObj = this;
                node.each(function () {
                    $(this).children('Form').each(function () {
                        var index = 0;
                        $(this).children('Widget').each(function () {
                            var widget = CreateWidget(id + '_' + index.toString(), $(this), curFormPageObj);
                            if(widget.formWidget.tabNum){
                            	self.formWidgets[widget.formWidget.tabNum - 1] = widget;
                            }else{
                            	widget.formWidget.tabNum = index;
                            	self.formWidgets[index] = widget;
                            }
                            index++;
                        });

                    });
                });
            }
        };

        this.getHtmlCode = function (bHighlight) {
            if (this.m_bLastPage) {
                var style = String.Format(strFormatArr.styleLTWH + 'position:absolute;background-color:transparent;', 0, 0, this.divWidth, this.divHeight);
                var divCode = '<div class="border" id="{0}" style="{1}">' +
                    '<button id="btnreset" style="margin:20px 50px;" class="fwr_button blue fwr_dialog_btn_cancel fr" name="btnreset" type="button">{2}</button>' +
                    '<button id="btnsubmit" style="margin:20px 0px;" class="fwr_button orange fwr_dialog_btn_ok fr" name="btnsubmit" type="button">{3}</button></div>';
                return String.Format(divCode, this.strID, style, strArr.resetText, strArr.submitText);
            }

            this.divWidth = this.pageWidth * ConfigInfo.PointToPx;
            this.divHeight = this.pageHeight * ConfigInfo.PointToPx;
            var style = String.Format(strFormatArr.styleLTWH + 'position:absolute;', 0, 0, parseInt(this.divWidth + 0.5), parseInt(this.divHeight + 0.5));

            var strCode = String.Format('<div id="{0}" class="' + formFieldsContainerClass +'" style="background-color:transparent;{1}">', this.strID, style);
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    strCode += this.formWidgets[i].getHtmlCode(bHighlight);
                }
            }
            strCode += '</div>';
            return strCode;
        };

        this.getRegisterActionCode = function () {
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].formWidget.getRegisterActionCode();
                }
            }
        };

        this.getReadyBindCode = function () {
            if (this.m_bLastPage) {
                $('#btnreset').bind('click', function () {
                    resetForm();
                    return false;
                });

                $('#btnsubmit').bind('click', function () {
                    submitForm();
                    return false;
                });
            }

            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].getReadyBindCode();
                }
            }
        };

        this.changeSize = function (scaleValue, bForced) {
            if (!bForced && scaleValue == this.scale) {
                return;
            }
            this.scale = scaleValue;
            var width = parseInt(this.divWidth * scaleValue + 0.5);
            var height = parseInt(this.divHeight * scaleValue + 0.5);
            setStyleLTWH($('#' + this.strID), 0, 0, width, height);
            if (this.m_bLastPage) {
                var margin20 = parseInt(20 * scaleValue + 0.5);
                var margin50 = parseInt(50 * scaleValue + 0.5);
                var fontSize = parseInt(11 * scaleValue + 0.5);
                var butHeight = parseInt(25 * scaleValue + 0.5);
                var butWidth = parseInt(80 * scaleValue + 0.5);
                $('#btnreset').css('margin', margin20 + 'px ' + margin50 + 'px').css('font-size', fontSize + 'px').css('height', butHeight + 'px').css('width', butWidth + 'px').css('min-width', butWidth + 'px');
                $('#btnsubmit').css('margin', margin20 + 'px 0px').css('font-size', fontSize + 'px').css('height', butHeight + 'px').css('width', butWidth + 'px').css('min-width', butWidth + 'px');
            } else {
                for (var i = 0; i < this.formWidgets.length; i++) {
                    if (this.formWidgets[i]) {
                        this.formWidgets[i].changeSize(scaleValue);
                    }
                }
            }
        };

        this.ShowPage = function () {
            $('#' + this.strID).show();
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].update();
                }
            }
        };

        this.HidePage = function () {
            $('#' + this.strID).hide();
        };

        this.changeHighlight = function (bHighlight) {
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].changeHighlight(bHighlight);
                }
            }
        };

        this.resetData = function () {
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].resetData();
                }
            }
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                    this.formWidgets[i].update();
                }
            }
        };

        /**
         * 依据WebPDF.g_formValue中值，更新各表单域
         *
         * 下面参数用于缩小更新查找范围
         * @param {String} form filed name
         * @param {String} element id
         *
         * @private
         */
        this.update = function () {
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                	if (!(arguments[0] && this.formWidgets[i].formWidget.name != arguments[0]))
                		this.formWidgets[i].update(arguments[1]);
                }
            }
        };
        
        function _isModifiedFormData(formWidgets, widget) {
        	var defaultValue = undefined;
        	var type = widget.formWidget.type;
        	var name = widget.formWidget.name;
        	if (type == Type.PushButton) {
        		return false;
        	} else if (type == Type.RadioButton || type == Type.CheckBox) {
        		defaultValue = "Off";
        		for (var i = 0; i < formWidgets.length; i++) {
        			if (formWidgets[i] && formWidgets[i].formWidget.name == name &&
        					formWidgets[i].bCheck) {
        				defaultValue = formWidgets[i].value;
        				break;
            		}
            	}
            } else if (type == Type.Text || type == Type.RichText || type == Type.ListBox) {
            	defaultValue = widget.value;
            } else if (type == Type.ComboBox) {
            	defaultValue = widget.selectValue ? widget.selectValue : widget.value;
            }
        	var value = WebPDF.g_formValue.GetItemValue(name);
        	return defaultValue != value;
        }
        
        this.isModifiedFormData = function () {
        	var checkedMap = {};
        	for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i]) {
                	var widgetName = this.formWidgets[i].formWidget.name;
            		if (checkedMap[widgetName]) {
                    	continue;
                    }
                    
                    if (_isModifiedFormData(this.formWidgets, this.formWidgets[i])) {
                    	return true;
                    }
                    checkedMap[widgetName] = true;
                }
            }
        	return false;
        };

        this.GetRadioGroupSelectedItemID = function (name) {
            var id = '';
            for (var i = 0; i < this.formWidgets.length; i++) {
                if (this.formWidgets[i] && this.formWidgets[i].formWidget.name == name) {
                    var tmpid = this.formWidgets[i].getControlID();
                    if ($('#' + tmpid).attr('isChecked') == '1') {
                        id = tmpid;
                        break;
                    }
                }
            }
            return id;
        };

        function _checkRequiredWidget(formWidgets, widget) {
        	var type = widget.formWidget.type;
        	var name = widget.formWidget.name;
        	if (type == Type.PushButton) {
        		return true;
        	}
        	
        	var value = WebPDF.g_formValue.GetItemValue(name);
        	if (value) {
        		var bCheck = true;
        		if (type == Type.RadioButton || type == Type.CheckBox) {
        			bCheck = false;
        			for (var i = 0; i < formWidgets.length; i++) {
            			if (formWidgets[i] && formWidgets[i].formWidget.name == name &&
            					formWidgets[i].value == value) {
            				bCheck = true;
            				break;
                		}
                	}
                }
        		
        		return bCheck;
        	}
        	return false;
        }
        
        this.CheckRequiredWidget = function () {
        	var checkedMap = {};
            for (var i = 0; i < this.formWidgets.length; i++) {
            	if (this.formWidgets[i] && this.formWidgets[i].formWidget.bRequired) {
                    var widgetName = this.formWidgets[i].formWidget.name;
            		if (checkedMap[widgetName]) {
                    	continue;
                    }
                    
                    if (!_checkRequiredWidget(this.formWidgets, this.formWidgets[i])) {
                    	return false;
                    }
                    checkedMap[widgetName] = true;
                }
            }
            return true;
        };

        this.Transform = function (pdf_l, pdf_b, pdf_w, pdf_h) {
            var retFloatRect = new FloatRect();

            switch (this.rotate) {
                //90
                case 1:
                {
                    retFloatRect.top = pdf_l - this.pdfRect.left;
                    retFloatRect.left = pdf_b - this.pdfRect.top;
                    retFloatRect.width = pdf_h;
                    retFloatRect.height = pdf_w;
                }
                    break;
                //180
                case 2:
                {
                    retFloatRect.top = pdf_b - this.pdfRect.top;
                    retFloatRect.left = this.pdfRect.width - pdf_l - this.pdfRect.left - pdf_w;
                    retFloatRect.width = pdf_w;
                    retFloatRect.height = pdf_h;
                }
                    break;
                case 3:
                    //270
                {
                    retFloatRect.top = this.pdfRect.width - (pdf_l - this.pdfRect.left) - pdf_w;
                    retFloatRect.left = this.pdfRect.height - (pdf_b - this.pdfRect.top) - pdf_h;
                    retFloatRect.width = pdf_h;
                    retFloatRect.height = pdf_w;
                }
                    break;
                default:
                    //0
                {
                    retFloatRect.top = this.pdfRect.height - (pdf_b - this.pdfRect.top) - pdf_h;
                    retFloatRect.left = pdf_l - this.pdfRect.left;
                    retFloatRect.width = pdf_w;
                    retFloatRect.height = pdf_h;
                }
            }

            return retFloatRect;
        };
    }

    /////////////////////////////////////////////////////////////////////////////////解析及显示 End

    /////////////////////////////////////////////////////////////////////////////////保存表单域值 Start
    //Form Filed Value
    //获取默认值
    //var g_formValue = new FormValue();
    function FormValue() {
        _formValueSelf = this;
        this.items = [];
        this.itemsActionJScriptChange = [];

        this.ClearAll = function () {
            this.items = [];
            this.itemsActionJScriptChange = [];
        };

        this.GetItemValue = function (name) {
            return this.items[name];
        };

        this.SetItemValue = function (name, value) {
            if (this.itemsActionJScriptChange[name]) {
                this.itemsActionJScriptChange[name] = false;
            } else {
                this.items[name] = value;
            }
        };

        this.SetItemValue_ActionJScript = function (name, value) {
            this.itemsActionJScriptChange[name] = true;
            this.items[name] = value;
        };

        this.GetFormDataXML = function () {
            var dataStr = '<?xml version="1.0" encoding="UTF-8"?><fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">';

            var index = 0;
            for (name in this.items) {
                if (typeof (this.items[name]) != 'undefined') {
                    var tag = 'FormField' + index.toString();
                    dataStr += String.Format('<{0} xfdf:original="{1}">{2}</{0}>', stringToXMLStr(tag), stringToXMLStr(name), stringToXMLStr(this.items[name].toString()));
                    index++;
                }
            }
            dataStr += '</fields>';
            return index == 0 ? '' : dataStr;
        };

        this.SetFormDataByXML = function (xmlDataObj) {
            xmlDataObj.children().each(function(){
                var name = getNodeString($(this), 'xfdf:original');
                var value = xmlStrToString($(this).text());
                _formValueSelf.items[name] = value;
            });
        };
    }

    /////////////////////////////////////////////////////////////////////////////////保存表单域值 End
    //PushButton
    var g_mouseLeftDown = false;
    var g_mouseOverArr = new Array();

    function PushButFgClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        setTimeout(function () {
            $('#' + event.data.id).focus().trigger('mouseover');
        }, 0);
    }

    function PushButFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        if ($('#' + event.data.id).hasClass(formWidgetClass)) {
            //将其z-index修改为form_widget_comboxlist
            $('#' + event.data.id).removeClass(formWidgetClass).addClass(formWidgetComboListClass);

            if (WebPDF.g_pFormPlugin.isHighlight()) {
                $('#' + event.data.fgid).css('display', 'none');
            }
	    
	     event.data.formWidget.onfocus = true;
        }
    }

    function PushButBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        setTimeout(function () {
            if (!document.activeElement.id || trim(document.activeElement.id).toLowerCase() != trim(event.data.id).toLowerCase()) {
                //将其z-index修改为fwr-form-widget-combo-list
                $('#' + event.data.id).removeClass(formWidgetComboListClass).addClass(formWidgetClass);

                if (WebPDF.g_pFormPlugin.isHighlight()) {
                    $('#' + event.data.fgid).css('display', '');
                }
		
		event.data.formWidget.onfocus = false;
            }
        }, 0);
    }

    function PushButMouseOut(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var key = trim(event.data.id).toLowerCase();
        //if(g_mouseOverArr[key] == true)
        //{
        if (!g_mouseLeftDown) {
            var curPageObj = WebPDF.g_pFormPlugin.getInitializedPageList()[event.data.curPageNum];
            var leftValue = 0 - parseInt(event.data.nImgPos * curPageObj.scale + 0.5);
            $('#' + event.data.id).css('left', leftValue + 'px');
        }
        g_mouseOverArr[key] = false;
        //}
    }

    function PushButMouseOver(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var key = trim(event.data.id).toLowerCase();
        //if(g_mouseOverArr[key] != true)
        //{
        if (!g_mouseLeftDown) {
            var curPageObj = WebPDF.g_pFormPlugin.getInitializedPageList()[event.data.curPageNum];
            var leftValue = 0 - parseInt(event.data.rImgPos * curPageObj.scale + 0.5);
            $('#' + event.data.id).css('left', leftValue + 'px');
        }
        g_mouseOverArr[key] = true;
        //}
    }

    function PushButMouseMove(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var key = trim(event.data.id).toLowerCase();
        //if(g_mouseOverArr[key] != true)
        //{
        if (!g_mouseLeftDown) {
            var curPageObj = WebPDF.g_pFormPlugin.getInitializedPageList()[event.data.curPageNum];
            var leftValue = 0 - parseInt(event.data.rImgPos * curPageObj.scale + 0.5);
            $('#' + event.data.id).css('left', leftValue + 'px');
        }
        g_mouseOverArr[key] = true;
        //}
    }

    function isMouseLeftButDown(event) {
        //文档模式 IE9不支持:arguments.caller.length
        var checkIE9 = null;
        try {
            checkIE9 = arguments.caller.length;
        } catch (e) {
        }

        var bLeftBtn = false;
        if (navigator.userAgent.indexOf('MSIE') > 0 && checkIE9 != null) {
            //IE9之前文档模式左键按钮是1
            if (event.button == 1)
                bLeftBtn = true;
        } else {
            if (event.button == 0)
                bLeftBtn = true;
        }
        return bLeftBtn;
    }

    function PushButMouseDown(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var key = trim(event.data.id).toLowerCase();
        //if(g_mouseOverArr[key] == true)
        //{
        if (isMouseLeftButDown(event)) {
            var element = document.getElementById(event.data.id);
            var curPageObj = WebPDF.g_pFormPlugin.getInitializedPageList()[event.data.curPageNum];
            var leftValueN = 0 - parseInt(event.data.nImgPos * curPageObj.scale + 0.5);
            var leftValueR = 0 - parseInt(event.data.rImgPos * curPageObj.scale + 0.5);
            var leftValueD = 0 - parseInt(event.data.dImgPos * curPageObj.scale + 0.5);
            $('#' + event.data.id).css('left', leftValueD + 'px');
            g_mouseLeftDown = true;

            if (event.preventDefault) {
                event.preventDefault();
            }

            if (window.captureEvents) {
                window.captureEvents(Event.MOUSEUP);
            } else if (element.setCapture) {
                element.setCapture();
            }

            var id = event.data.id;
            var d = document;
            d.onmouseup = function (e) {
                e = e || window.event;
                if (isMouseLeftButDown(e)) {
                    //取消捕获范围
                    if (window.releaseEvents) {
                        window.releaseEvents(Event.MOUSEUP);
                    } else if (element.releaseCapture) {
                        element.releaseCapture();
                    }

                    if (g_mouseOverArr[key] != true || navigator.userAgent.indexOf('MSIE') > 0 || navigator.userAgent.toLowerCase().indexOf('trident') > 0) {
                        $('#' + id).css('left', leftValueN + 'px');
                    } else {
                        $('#' + id).css('left', leftValueR + 'px');
                    }
                    g_mouseLeftDown = false;
                    //清除事件
                    d.onmouseup = null;
                }
            };
        }
        //}
    }

    ///end PushButton

    function CheckedObj(checkRaidoObj, bChecked) {
        var obj = $('#' + checkRaidoObj.getControlID());
        var bCurChecked = (obj.attr('isChecked') == '1');
        var imgLeft = 0 - parseInt((bChecked ? checkRaidoObj.onImgPos : checkRaidoObj.offImgPos) * checkRaidoObj.formWidget.curPageObj.scale + 0.5);
        if (bChecked && !bCurChecked) {
            obj.attr('isChecked', '1').css('left', imgLeft + 'px');
        } else if (!bChecked && bCurChecked) {
            obj.attr('isChecked', '0').css('left', imgLeft + 'px');
        }
    }

    function CheckRaidoButFgClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        setTimeout(function () {
            $('#' + event.data.id).focus().click();
        }, 0);
    }

    function CheckRaidoButFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        //将其z-index修改为form_widget_comboxlist
        $('#' + event.data.id).removeClass(formWidgetClass).addClass(formWidgetComboListClass);

        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', 'none');
        }
	
	event.data.formWidget.onfocus = true;
    }

    function CheckRaidoButBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        setTimeout(function () {
            if (!document.activeElement.id ||
                (trim(document.activeElement.id).toLowerCase() != trim(event.data.id).toLowerCase())) {
                //将其z-index修改为form_widget_comboxlist
                $('#' + event.data.id).removeClass(formWidgetComboListClass).addClass(formWidgetClass);

                if (WebPDF.g_pFormPlugin.isHighlight()) {
                    $('#' + event.data.fgid).css('display', '');
                }
		
		event.data.formWidget.onfocus = false;
            }
        }, 0);
    }

    //Check
    function CheckButClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        var value = 'Off';
        if (obj.attr('isChecked') != '1') {
            value = obj.attr('value');
        }
        var name = obj.attr('name');
        WebPDF.g_formValue.SetItemValue(name, value);
        WebPDF.g_pFormPlugin.updateForm(name);
    }

    //Raido
    function RaidoButClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        var name = obj.attr('name');
        if (obj.attr('isChecked') != '1') {
            WebPDF.g_formValue.SetItemValue(name, obj.attr('value'));
            WebPDF.g_pFormPlugin.updateForm(name, obj.attr('id'));
        } else if (!event.data.bNoToggleToOff) {
            WebPDF.g_formValue.SetItemValue(name, 'Off');
            WebPDF.g_pFormPlugin.updateForm(name, obj.attr('id'));
        }
    }

    //Text
    //传入要获取选择文本的input或者textarea对象ID
    function getSelectedText(objID) {
        var e = document.getElementById(objID);
        if (document.selection) {
            //IE下获取选择文本
            return document.selection.createRange().text;
        } else if (window.getSelection().toString()) {
            //firefox下获取选择文本
            return window.getSelection().toString();
        } else if (e.selectionStart != undefined && e.selectionEnd != undefined) {
            //firefox下获取input或textArea域的选择文本
            var start = e.selectionStart;
            var end = e.selectionEnd;
            return e.value.substring(start, end);
        }
    }

    function spellCeck(value, bTextarea, obj, spanObj) {
        //解决空格测不出长度问题;
        var str = value.replace(/ /g, '&nbsp;');
        var objWidth = obj.width();
        if (bTextarea) {
            var rowCount = parseInt(obj.height() / parseFloat(obj.css('font-size')));
            objWidth = objWidth * rowCount;
            while (rowCount > 1) {
                //占位,供计算
                str += 'M';
                rowCount -= 1;
            }
        }
        spanObj.html(str);
        return spanObj.width() > objWidth;
    }

    function TextSpellCeck(event) {
        if (event.ctrlKey) {
            return;
        }
        var code;
        if (document.all) {
            //判断是否是IE浏览器
            code = window.event.keyCode;
        } else {
            code = arguments.callee.caller.arguments[0].which;
        }
        if (code == 8 || code == 46) {
            return true;
        }

        var character = String.fromCharCode(code);
        var obj = $('#' + event.data.id);
        var bUpperLimit = false;
        var selectedStr = getSelectedText(event.data.id);
        var str = '';
        if (selectedStr && selectedStr != '') {
            str = obj.val().replace(selectedStr, character);
        } else {
            str = obj.val() + character;
        }
        var spanCode = String.Format('<span id= "SpellCeckSpan" style= "visibility:hidden;border:0;padding:0;font-size:{0};"></span>', obj.css('font-size'));
        obj.after(spanCode);
        var tmpSpanObj = $('#SpellCeckSpan');
        bUpperLimit = spellCeck(str, event.data.bTextarea, obj, tmpSpanObj);
        tmpSpanObj.remove();

        //特殊字符正则表达式
        if (bUpperLimit) {
            if (document.all) {
                window.event.returnValue = false;
                return false;
            } else {
                arguments.callee.caller.arguments[0].preventDefault();
                return true;
            }
        }
    }

    function checkPasteTextWidth(id, bTextarea) {
        var obj = $('#' + id);
        var value = obj.val();

        var spanCode = String.Format('<span id= "SpellCeckSpan" style= "visibility:hidden;border:0;padding:0;font-size:{0};"></span>', obj.css('font-size'));
        obj.after(spanCode);
        var tmpSpanObj = $('#SpellCeckSpan');

        //while (true) {
        for (var ii = 0; ii < 1;) {
            if (spellCeck(value, bTextarea, obj, tmpSpanObj)) {
                value = value.substr(0, value.length - 1);
            } else {
                break;
            }
        }
        tmpSpanObj.remove();
        obj.val(value);
    }

    function TextPaste(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        var beforeValue = obj.val();
        var bUpperLimit = false;

        var spanCode = String.Format('<span id= "SpellCeckSpan" style= "visibility:hidden;border:0;padding:0;font-size:{0};"></span>', obj.css('font-size'));
        obj.after(spanCode);
        var tmpSpanObj = $('#SpellCeckSpan');
        bUpperLimit = spellCeck(beforeValue + beforeValue.substr(beforeValue.length - 1), event.data.bTextarea, obj, tmpSpanObj);
        tmpSpanObj.remove();

        var selectedStr = getSelectedText(event.data.id);
        if (bUpperLimit && !selectedStr) {
            if (document.all) {
                window.event.returnValue = false;
                return false;
            } else {
                arguments.callee.caller.arguments[0].preventDefault();
                return true;
            }
        } else {
            setTimeout(function () {
                checkPasteTextWidth(event.data.id, event.data.bTextarea);
            }, 0);
        }
    }

    function FgClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        setTimeout(function () {
            $('#' + event.data.id).focus();
        }, 0);
    }

    function getLocation(elmid) {
        var elm = document.getElementById(elmid);
        if (elm.createTextRange) {
            // IE
            var range = document.selection.createRange();
            range.setEndPoint('StartToStart', elm.createTextRange());
            return range.text.length;
        } else if (typeof elm.selectionStart == 'number') {
            // Firefox
            return elm.selectionStart;
        }
    }

    function setLocation(elmid, n) {
        var elm = document.getElementById(elmid);
        if (n > elm.value.length)
            n = elm.value.length;
        if (elm.createTextRange) {
            // IE 
            var textRange = elm.createTextRange();
            textRange.moveStart('character', n);
            textRange.collapse();
            textRange.select();
        } else if (elm.setSelectionRange) {
            // Firefox 
            elm.setSelectionRange(n, n);
            elm.focus();
        }
    }

    function getCursorPosition(textarea) {
        var pos = 0;
        textarea.focus();
        if (textarea.setSelectionRange) {
            // W3C
            pos = textarea.selectionStart;
        } else if (document.selection) {
            // IE
            var i,
                oS = document.selection.createRange(),
                oR = document.body.createTextRange();
            oR.moveToElementText(textarea);

            for (i = 0; oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart('character', -1) !== 0; i++) {
                if (textarea.value.charAt(i) == '\n') {
                    i++;
                }
            }
            pos = i;
        }
        return pos;
    }

    function setCursorPosition(textarea, pos) {
        if (textarea.setSelectionRange) {
            // W3C
            textarea.focus();
            textarea.setSelectionRange(pos, pos);
        } else if (textarea.createTextRange) {
            // IE
            var oR = textarea.createTextRange();
            // Fixbug :        
            // In IE, if cursor position at the end of textarea, the setCursorPosition function don't work        
            if (textarea.value.length === pos) {
                oR.collapse(false);
                oR.select();
            } else {
                oR.select();
            }
        }
    }

    function TextareaFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        if (event.data.bReadOnly) {
            if (!event.data.bSpellCheck) {
                obj.removeClass(formTextAreaNoneBorderClass);
            }
            return;
        }

        //将其z-index修改为fwr-form-widget-combo-list
        obj.removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        $('#' + event.data.bgid).removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        if(event.data.formWidget)event.data.formWidget.onfocus = true;
        var name = obj.attr('name');
        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', 'none');
            if (!event.data.bSpellCheck) {
                obj.removeClass(formTextAreaNoneBorderClass);
            }
            obj.val(WebPDF.g_formValue.GetItemValue(name));
        } else {
            var index = getCursorPosition(document.getElementById(event.data.id));
            if (!event.data.bSpellCheck) {
                obj.removeClass(formTextAreaNoneBorderClass);
            }
            obj.val(WebPDF.g_formValue.GetItemValue(name));
            setCursorPosition(document.getElementById(event.data.id), index);
        }
    }

    function TextareaBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        if (event.data.bReadOnly) {
            obj.addClass(formTextAreaNoneBorderClass);
            return;
        }

        //将其z-index修改为form_widget
        obj.removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        $('#' + event.data.bgid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        if(event.data.formWidget)event.data.formWidget.onfocus = false;
        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', '');
        }
        obj.addClass(formTextAreaNoneBorderClass);
        var name = obj.attr('name');
        WebPDF.g_formValue.SetItemValue(name, obj.val());
        if (obj.attr('noupdate') == 'true') {
            obj.attr('noupdate', '');
        } else {
            WebPDF.g_pFormPlugin.updateForm(name, obj.attr('id'));
        }
        obj.triggerJSAction();
       // WebPDF.g_pFormPlugin.getReaderApp().setModified(null, null, true);
    }

    function TextFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        //将其z-index修改为fwr-form-widget-combo-list
        obj.removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        $('#' + event.data.bgid).removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        if(event.data.formWidget)event.data.formWidget.onfocus = true;

        var name = obj.attr('name');
        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', 'none');
            obj.attr('value', WebPDF.g_formValue.GetItemValue(name));
        } else {
            var index = getLocation(event.data.id);
            obj.attr('value', WebPDF.g_formValue.GetItemValue(name));
            setLocation(event.data.id, index);
        }
    }

    function TextBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var obj = $('#' + event.data.id);
        //将其z-index修改为form_widget
        obj.removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        $('#' + event.data.bgid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        if(event.data.formWidget)event.data.formWidget.onfocus = false;
        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', '');
        }

        var name = obj.attr('name');
        WebPDF.g_formValue.SetItemValue(name, obj.val());
        if (obj.attr('noupdate') == 'true') {
            obj.attr('noupdate', '');
        } else {
            WebPDF.g_pFormPlugin.updateForm(name, obj.attr('id'));
        }
        obj.triggerJSAction();
    //    WebPDF.g_pFormPlugin.getReaderApp().setModified(null, null, true);
    }

    //Combo
    function SelectComboItem(jquer_obj, bSelected) {
        if (bSelected && (jquer_obj.attr('isSelected') != '1')) {
            jquer_obj.attr('isSelected', '1').addClass(formListItemSelectedClass);
        } else if ((!bSelected) && (jquer_obj.attr('isSelected') != '0')) {
            jquer_obj.attr('isSelected', '0').removeClass(formListItemSelectedClass);
        }
     //   WebPDF.g_pFormPlugin.getReaderApp().setModified(null, null, true);
    }

    function ComboButFocusOrFgFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        event.data.formWidget.onfocus = true;
        setTimeout(function () {
            $('#' + event.data.inputid).focus();
        }, 0);
    }

    function ComboButFocusOrFgClick(event,data) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        event.data.formWidget.onfocus = true;
        setTimeout(function () {
            if(event.data)$('#' + event.data.inputid).focus().click();
            else $('#' + data.inputid).focus().click();
        }, 0);
    }

    function ComboButIputFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        //将其z-index修改为form_widget_comboxlist
        $('#' + event.data.id).removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        $('#' + event.data.bgid).removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        $('#' + event.data.inputid).removeClass(formWidgetClass).addClass(formWidgetComboListClass);

        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', 'none');
        }
    }

    function ComboButIputClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        $('#' + event.data.listdivid).show();
    }

    function ComboButIputBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        event.data.formWidget.onfocus = false;
        setTimeout(function () {
            if (!document.activeElement.id ||
                (trim(document.activeElement.id).toLowerCase() != trim(event.data.inputid).toLowerCase() &&
                    trim(document.activeElement.id).toLowerCase() != trim(event.data.id).toLowerCase() &&
                    trim(document.activeElement.id).toLowerCase().indexOf(trim(event.data.id).toLowerCase()) == -1)) {
                var combobutobj = $('#' + event.data.id);
                var inputObj = $('#' + event.data.inputid);
                var inputRealValue = inputObj.attr('realValue');
                if (inputObj.val() != inputObj.attr('oldValue')) {
                    inputRealValue = inputObj.val();
                }
                var name = combobutobj.attr('name');
                WebPDF.g_formValue.SetItemValue(name, inputRealValue);
                WebPDF.g_pFormPlugin.updateForm(name);
                $('#' + event.data.listdivid).hide();

                //将其z-index修改为form_widget
                $('#' + event.data.id).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
                $('#' + event.data.bgid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
                $('#' + event.data.inputid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);

                if (WebPDF.g_pFormPlugin.isHighlight()) {
                    $('#' + event.data.fgid).css('display', '');
                }
            }
        }, 0);
    }

    function ComboListItemClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var curobj = $('#' + event.data.id);
        if (curobj.attr('selected') != '1') {
            var combobutobj = $('#' + event.data.combodivid);
            var name = combobutobj.attr('name');
            WebPDF.g_formValue.SetItemValue(name, curobj.attr('realValue'));
            WebPDF.g_pFormPlugin.updateForm(name);
        }
        
        $('#' + event.data.listdivid).hide();

        //将其z-index修改为form_widget
        $('#' + event.data.combodivid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        $('#' + event.data.bgid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
        $('#' + event.data.inputid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);

        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', '');
        }
    }

    //List
    function getX(obj) {
        var ParentObj = obj;
        var left = obj.offsetLeft;
        while (ParentObj = ParentObj.offsetParent) {
            if (ParentObj.offsetLeft < 0) {
                continue;
            }
            left += ParentObj.offsetLeft;
        }
        return left;
    }

    function getY(obj) {
        var ParentObj = obj;
        var top = obj.offsetTop;
        while (ParentObj = ParentObj.offsetParent) {
            if (ParentObj.offsetTop < 0) {
                continue;
            }
            top += ParentObj.offsetTop;
        }
        return top;
    }

    function ListBoxFgClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var oDiv = document.getElementById(event.data.id);
        var pane = $('.scroll-pane');
        var api = pane.data('jsp');
        //var left = (event.clientX - getX(oDiv) + api.getContentPositionX()) - 2;
        var top = (event.clientY - getY(oDiv) + api.getContentPositionY()) - 2;

        var clickid = '';
        if (event.data.optiosCount > 0) {
            var listObj = $('#' + event.data.id);
            var listItemObjId = event.data.id + '_0';
            var itemHeight = parseFloat($('#' + listItemObjId).css('height'));
            if (!itemHeight) {
                //IE7\IE8
                itemHeight = parseFloat(document.getElementById(listItemObjId).scrollHeight);
            }

            var index = parseInt((top + listObj.scrollTop()) / itemHeight) + 1;
            if ((0 < index) && (index <= event.data.optiosCount)) {
                clickid = event.data.id + '_' + (index - 1);
            }
        }

        setTimeout(function () {
            $('#' + event.data.id).focus();
            if (clickid && clickid != '') {
                g_bFgClickAndCtrlKey = event.ctrlKey;
                $('#' + clickid).click();
            }
        }, 0);
    }

    function ListFocus(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        if(event.data.formWidget)event.data.formWidget.onfocus = true;
        //将其z-index修改为form_widget_comboxlist
        var obj = $('#' + event.data.id);
        obj.removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        obj.children().each(function () {
            $(this).removeClass(formWidgetClass).addClass(formWidgetComboListClass);
        });
        $('#' + event.data.bgid).removeClass(formWidgetClass).addClass(formWidgetComboListClass);

        if (WebPDF.g_pFormPlugin.isHighlight()) {
            $('#' + event.data.fgid).css('display', 'none');
        }
    }

    function ListBlur(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        if(event.data.formWidget)event.data.formWidget.onfocus = false;
        setTimeout(function () {
            var index = -1;
            if (document.activeElement.id) {
                var focusObjID = trim(document.activeElement.id).toLowerCase();
                var listObjID = trim(event.data.id).toLowerCase();
                index = focusObjID.lastIndexOf(listObjID);
            }
            if (index < 0) {
                //将其z-index修改为form_widget
                var obj = $('#' + event.data.id);
                obj.removeClass(formWidgetComboListClass).addClass(formWidgetClass);
                obj.children().each(function () {
                    $(this).removeClass(formWidgetComboListClass).addClass(formWidgetClass);
                });
                $('#' + event.data.bgid).removeClass(formWidgetComboListClass).addClass(formWidgetClass);

                if (WebPDF.g_pFormPlugin.isHighlight()) {
                    $('#' + event.data.fgid).css('display', '');
                }
            }
        }, 0);
    }

    var g_bFgClickAndCtrlKey = false;

    function ListItemClick(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;
        //在FgClick触发该事件时，是否按了Ctrl
        var bCtrlKey = g_bFgClickAndCtrlKey;
        g_bFgClickAndCtrlKey = false;
        var curObj = $('#' + event.data.id);
        var listObj = $('#' + event.data.listdivid);
        var bMultiple = listObj.attr('fxmultiple') == 'true' ? true : false;
        var bCurObjSelected = curObj.attr('isSelected') == '1' ? true : false;
        var selectedObjValue = curObj.attr('realValue');

        if (!bMultiple && bCurObjSelected) {
            return;
        } else {
            listObj.children().each(function () {
                if (bMultiple && (event.ctrlKey || bCtrlKey)) {
                    if ($(this).attr('realValue') == selectedObjValue) {
                        SelectComboItem($(this), !bCurObjSelected);
                    }
                } else {
                    SelectComboItem($(this), $(this).attr('realValue') == selectedObjValue);
                }
            });

            var value = '';
            listObj.children().each(function () {
                if ($(this).attr('isSelected') == '1') {
                    if (value != '') {
                        value += ';';
                    }
                    value += trim($(this).attr('realValue'));
                }
            });
            var name = listObj.attr('name');
            WebPDF.g_formValue.SetItemValue(name, value);
            WebPDF.g_pFormPlugin.updateForm(name);
        }
    }

    function ListItemKeydown(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var curObj = $('#' + event.data.id);
        var listObj = $('#' + event.data.listdivid);
        var selectObj = null;
        var keyValue = event.keyCode || event.which;
        if (keyValue == 38) {
            //向上键
            selectObj = curObj.prev();
        } else if (keyValue == 40) {
            //向下键
            selectObj = curObj.next();
        }

        if (selectObj && selectObj.attr('tagName')) {
        	var name = listObj.attr('name');
            WebPDF.g_formValue.SetItemValue(name, selectObj.attr('realValue'));
            WebPDF.g_pFormPlugin.updateForm(name);
            selectObj.focus();
        }
        return false;
    }

    function ListKeyDown(event) {
        if (!WebPDF.g_pFormPlugin.isFormFieldsEnable()) return;

        var firstObj = null;
        var selectedObj = null;
        var bPre = false;
        var keyValue = event.keyCode || event.which;
        if (keyValue == 38) {
            //向上键
            bPre = true;
        } else if (keyValue == 40) {
            //向下键
            bPre = false;
        } else {
            return false;
        }
        ////获取被选中的对象
        var listObj = $('#' + event.data.id);
        listObj.children().each(function () {
            if (!firstObj) {
                firstObj = $(this);
            }
            if (!selectedObj) {
                if ($(this).attr('isSelected') == '1') {
                    selectedObj = $(this);
                }
            }
            if (firstObj && selectedObj) {
                return false;
            }
        });


        //计算将要被选中的对象
        var selectObj;
        if (selectedObj) {
            if (bPre) {
                //向上键
                selectObj = selectedObj.prev();
            } else {
                //向下键
                selectObj = selectedObj.next();
            }
            if (selectObj && !selectObj.attr('tagName')) {
                selectObj = selectedObj;
            }
        } else {
            selectObj = firstObj;
        }
        /////选择改变，同时清除其他选择
        if (selectObj) {
        	var name = listObj.attr('name');
            WebPDF.g_formValue.SetItemValue(name, selectObj.attr('realValue'));
            WebPDF.g_pFormPlugin.updateForm(name);
        }
        return false;
    }
    /////////////////////////////////////////////////////////////////////////////////表单控件消息处理 End

    WebPDF.FormPage = FormPage;
    WebPDF.FormValue = FormValue;
});
